Search engine won"t page properly
am 04.02.2011 05:33:24 von Bill MudryThe website I have been working on (as time permits) reports over Please enter a search string... We dont seem to have a search parameter! Sorry, your search: "" .=20 You searched for: ""=20 << Prev $limit
15,000 woods. With that much data, an internal search engine has
become mandatory for users to find what they are looking for. Almost
all the pages are dynamically formed using PHP and data stored in
MySQL files.
Therefore, a search engine that works on hard coded pages would be
totally unusable. I needed one that can search through MySQL tables.
I found a decent template for such a search engine at:
http://www.designplace.org/scripts.php?page=3D1&c_id=3D25
The original template works on searching one column in one table and
reporting only from that one column. The part of the data that by far
most users will wish to search is the species table. I wanted the search
report to show data keyed by one column but displaying multiple
columns --- so I knew I had some additional coding to do..
I
A while back I actually got a copy working for a while, broke it trying to
change it over to working with multiple column choices and then didn't
have a copy left of the code that worked.
I have come a fair way to getting it to work but it won't page properly.
It has a parameter ($limit, presently set to 10 records) to control how
many records can be printed per page). It prints one page but gets
stuck. It has "<
forward in pages.
I have put in hours heavily adding internal documentation, studying the
logic till I understood most of it and plastering ECHO statements on
parameters to understand what is happening.
I think I know now what is wrong. $s is the record counter. Once its
value in multiples has a run over for the current page, a new page is
created by incrementing the record counter as the low end record for
the next page for forward travel and decrementing=20
for going backwards. The next page (or prior=20
page) is printed using $PHP_SELF statements
to run the same query again but in the prior or next page.
It took a lot of study , but as far as I can tell (..... and prove me wrong=
if
I am), when the program is turned back on itself=20
(using $PHP_SELF statements, whether for=20
incrementing or decrementing pages), the
value of $s near the top of the program gets lost. It has no chance to
accumulate (or reduce for backward travel). Therefore the program
gets locked into showing only the first page.
To test this, I hard set $s to a higher value, and ran a query. It
responded as it should, now reporting higher record numbers. As far as
I can see, repairing the paging problem seems to hinge on allowing
$s to carry over to be usable at the top of the program when
$PHP_SELF turns the program on itself.
What I am not familiar with is what the best way is to do that. What do
you suggest? Including code example if possible would be helpful?
If you also happen to spot any other serious bugs, please also let me
know. I am largely confident once $s can carry forward on each
program reiteration, the search engine will work as it should, good
paging included.
Sharing the Near Future
I have already done quite a bit of work on code (that works) that allows
users to choose what column to search on (eg. common names,
botanical names, country of origin, etc.). Once this paging problem is
overcome, It should be fairly easy to expand the search engine to work
on multiple search categories (ie. columns). By that point, it will make
the knowledge base website immensely more powerful and useful
for users to get versatile access to all its stored data. I really look
forward to that!
I will include all the code for this page below. I am also keeping the
large number of debug statements in and active and showing on the
output in a browser when the program is run --- so you can follow what
values parameters have at each step. The search page can be
directly viewed at:
http://www.prowebcanada.com/taxa/commonname_search.php?query string=3DOak&Sub=
mit=3DSearch
Much thanks. Your help makes it possible to go forward where
otherwise I would have a hard or impassible time.
Bill Mudry
Mississauga, Ontario
==================== =====3D=
==================== =====3D=
==
[CODE]
//$s =3D 14; hard coded as a debug statement
$S=3D @$_GET['$s'] ;
Echo "\$s on line 69 is - $s
"; //debug statement
?>
Simple Common Name Search
// Get the search variable from URL
$newquerystring =3D @$_GET['querystring'] ;
$querystring =3D trim($newquerystring); //trim=20
whitespace from the stored variable
=
/*===================3D= 3D=====
==================== =====3D=
====================
XXXXXXXXXXXXX SET THE NUMBER OF RECORDS PER PAGE HERE =
XXXXXXXXXXXXXX
=
==================== =====3D=
==================== =====3D=
===================3D*/
$limit=3D'10';
// check for an empty string and display a message.
if ($querystring == "")
{
echo "
exit;
}
// check for a search parameter
if (!isset($newquerystring))
{
echo "
exit;
}
/*===================3D= 3D=====
==================== =====3D=
==================== =====3D=
===============3D
XXXXXXXXXXXXXXXXXXXXXXXXXXX Connect to the=20
TAXA Database XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
=
==================== =====3D=
==================== =====3D=
==================== =====3D=
==============*/
include ("connecttotaxa.php");
$connection =3D mysql_connect($hostname, $username, $password)
or die("Unable to connect to database server");
//specify database ** EDIT REQUIRED HERE **
mysql_select_db("taxa") or die("Unable to select=20
the TAXA database"); //select which database we're using
$searchfield =3D 'species_commonname';
//$searchfield =3D 'species_name';
//$searchfield =3D 'genus_name';
//$searchfield =3D 'species_description';
//$searchfield =3D 'location';
/*===================3D= 3D=====
==================== =====3D=
========
XXXXXXXXXXXXXXX BUILD THE SQL QUERY XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
=======3D*/
// Build SQL Query
$searchquery =3D "SELECT *
FROM species
WHERE $searchfield like \"%$querystring%\"
order by '$searchfield'";
$numresults=3Dmysql_query($searchquery);
$numrows=3Dmysql_num_rows($numresults);
/*===================3D= 3D=====
==================== =====3D=
==================== =====3D=
==================== =3D
XXXXXXXXXXXXXXX TEST FOR ZERO RESULTS AND=20
PRINT A MESSAGE OF THAT IF TRUE XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
==================== =====3D=
==================== =3D*/
if ($numrows == 0)
{
echo "Search Results
";
echo "
$querystring. "" returned zero results
}
/*===================3D= 3D=====
==================== =====3D=
========
// $S is the record counter for the present record
// next determine if s has been passed to script, if not use 0
==================== =====3D=
==================== =====3D=
=======3D*/
if (empty($s)) {
$s=3D0;
}
/*===================3D= 3D=====
==================== =====3D=
==================== =====3D=
=3D
XXXXXXXXXXXXXXX QUERY THE DATABASE TO GET THE RESULTS =
XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
==================== =====3D*=
/
// get results
$searchquery .=3D " limit $s,$limit";
$result =3D mysql_query($searchquery) or die("Couldn't execute query");
/*===================3D= 3D=====
==================== =====3D=
==================== =====3D=
===============3D
XXXXXXXXXXXXXXX ECHO BACK TO THE USER WHAT=20
THEY ASKED TO SEARCH FOR XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
==================== =====3D=
==============*/
echo "
.. $querystring . ""
/*===================3D= 3D=====
==================== =====3D=
========
XXXXXXXXXXXXXXX SET A TITLE FOR THE REPORT XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
=======3D*/
echo "Search Results
";
// ADD 1 TO THE COUNTER FOR THE LATEST RECORD PRINTED FOR THE PRESENT PAGE
$count =3D 1 + $s ;
// now you can display the results returned
/*===================3D= 3D=====
==================== =====3D=
==================== =====3D
XXXXXXXXXXXXXXX SET UP COLUMN HEADERS FOR THE REPORT =
XXXXXXXXXXXXXXXXXXXX
==================== =====3D=
==================== =====3D=
==================== ====*/
Echo "=
";
";
Echo ""; ";
Echo ""; ";
Echo "Common Name";
Echo "
Echo ""; ";
Echo "Botanical Name";
Echo "
Echo ""; ";
Echo "Authorities";
Echo "
Echo ""; ";
Echo "Genus Name";
Echo "
Echo ""; ";
Echo "Location";Echo "
Echo ""; ";
Echo "Comments";
Echo "
Echo "
Echo ""; ";
//Echo "";
/*===================3D= 3D=====
==================== =====3D=
=======3D
XXXXXXXXX PRINT OUT RECORDS TILL END OF QUERY XXXXXXXXX
************** WHILE LOOP STARTS HERE ******************
==================== =====3D=
==================== =====3D=
======*/
while ($row=3D mysql_fetch_array($result)) {
$commonname =3D $row["species_commonname"];
$botname =3D $row["species_name"];
$authorities =3D $row["authorities_species"];
$genusname =3D $row["genus_name"];
$location =3D $row["location"];
$comments =3D $row["comments"];
ECHO ""; ";
ECHO "$count. $commonname
" ;
ECHO "
ECHO ""; ";
ECHO "";
ECHO "$botname
";
ECHO ".
\n";
ECHO "
ECHO ""; ";
ECHO "$authorities
";
ECHO "
ECHO ""; ";
ECHO "$genusname
";
ECHO "
ECHO ""; ";
ECHO "$location
";
ECHO "
ECHO ""; ";
ECHO "$comments
";
ECHO "
Echo "
//Echo ""; ";
//ECHO "";
$count++ ;
$s++ ; // DOESN'T $S HAVE TO BE INCREMEENTED FOR EACH ROW PRINTED ALSO??
// ANOTHER COPY WORKED A FEW MONTHS AGO=20
WITHOUT IT. iT IS NOT IN THE TEMPLATE.
Echo "\$s on line 265 is - $s
"; //debug statement
}
/*===================3D= 3D=====
==================== =====3D=
=======3D
XXXXXXXXX END OF WHILE LOOP. XXXXXXXXX
==================== =====3D=
==================== =====3D=
======*/
Echo "\$s on line 272 is - $s
"; //debug statement
////////////////////////////////////////////////////////////
// END OF WHILE LOOP.
////////////////////////////////////////////////////////////
Echo "
Echo "
////////////////////////////////////////////////////////////
// CLOSE THE REPORT TABLE
////////////////////////////////////////////////////////////
ECHO "
";
Echo "\$s on line 288 is - $s
"; //debug statement
$currPage =3D (($s/$limit) + 1);
//$pages =3D $currPage; //
ECHO "\$currPage on line 292 is - $currPage
"; //debug statement
Echo "\$s on line 286 is - $s
"; //debug statement
//break before paging
echo "
";
// next we need to do the links to other results
/*===================3D= 3D=====
==================== =====3D=
========----------
// If $s is less than 1, there are no more records to view backwards.
==================== =====3D=
==================== =====3D=
=================3D*/
if ($s>=3D1) { // bypass PREV link if s is 0
ECHO "\$s at line 297 is - $s
"; //debug statement
$prevs=3D($s-$limit);
////////////////////////////////////////////////////////////
// This statement will run the prior page of the report.
// It is user chosen
////////////////////////////////////////////////////////////
//print " <=
<
print "
">
}
// calculate number of pages needing links
$pages=3Dintval($numrows/$limit);
ECHO "\$pages on line 313 is - $pages
"; //debug statement
ECHO "\$numrows on line 313 is - $numrows
"; //debug statement
ECHO "\$limit on line 313 is - $limit
"; //debug statement
// $pages now contains int of pages needed unless=20
there is a remainder from division
if ($numrows%$limit) {
// has remainder so add one page
$pages++;
}
ECHO "\$pages on line 325 is - $pages
"; //debug statement
$remainder =3D ($numrows%$limit);
ECHO "\$remainder on line 327 is - $remainder
"; //debug statement
ECHO "
";
//$x =3D ($s+$limit)/$limit
//ECHO "\$x on line 296 is - $x
"; //debug statement
ECHO "\$s on line 332 is - $s
"; //debug statement
ECHO "\$limit on line 332 is - $limit
"; //debug statement
ECHO "\$pages on line 332 is - $pages
"; //debug statement
// check to see if last page
if (!((($s+$limit)/$limit)==$pages) && $pages!=3D1) {
ECHO "
";
// not last page so give NEXT link
$news=3D$s+$limit;
ECHO "\$s on line 343 is - $s
"; //debug statement
ECHO "\$limit on line 343 is - $limit
"; //debug statement
ECHO "\$news on line 343 is - $news
"; //debug statement
ECHO "
////////////////////////////////////////////////////////////
// This statement will run the next page of the report.
// It is user chosen
////////////////////////////////////////////////////////////
echo "
href=3D\"$PHP_SELF?s=3D$news&querystring=3D$querystring\">
class=3D'largertext' aligm=3D'right'>Next $limit >>";
ECHO "
}
Echo "\$s on line 366 is - $s
"; //debug statement
?>
[/CODE]
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php